package com.example.blog_system_ssm.component;

import com.example.blog_system_ssm.entity.UserEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class UserSessionTools {

    // 获取当前登录用户 优点：高效，从Session中读取两次
    public static UserEntity getLoginUser(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(ApplicationVariable.SESSION_KEY_USERINFO) != null){
            return (UserEntity) session.getAttribute(ApplicationVariable.SESSION_KEY_USERINFO);
        }
        return null;
    }

    // 获取当前登录用户 优点：高效，只从Session中读取一次
    public static UserEntity getLoginUser1(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        Object userEntity = null;
        if(session != null && (userEntity = session.getAttribute(ApplicationVariable.SESSION_KEY_USERINFO)) != null){
            return (UserEntity) userEntity;
        }
        return null;
    }
}
